package com.app.service.impl;

import com.app.mapper.ComputerMapper;
import com.app.model.ComputerModel;
import com.app.service.ComputerService;
import com.app.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ListUtils;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Primary
public class ComputerServiceImpl implements ComputerService {
    @Autowired
    ComputerMapper computerMapper;

    @Override
    public ComputerModel getComByComId(int id) {
        QueryWrapper<ComputerModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().eq("id", id);
        ComputerModel role = computerMapper.selectOne(queryWrapper);
        return role;
    }

    @Override
    public Page<ComputerModel> getPageComList(int page, int limit, String name, Integer departmentId, String personName, List<Integer> idsList, Integer type, Integer sortType) {
        QueryWrapper<ComputerModel> queryWrapper = new QueryWrapper<>();
        if (idsList.isEmpty()) {
            queryWrapper.select();
        } else {
            queryWrapper.in("department_id", idsList);
        }
        if (departmentId != null) {
            queryWrapper.eq("department_id", departmentId);
        }
        if (personName != null) {
            queryWrapper.like("person_name", personName);
        }
        if (type != null) {
            queryWrapper.eq("type", type);
        }
        if (name != null && !name.isEmpty()) {
            queryWrapper.like("name", name);
        }
        if (sortType==null || sortType==1){
            queryWrapper.orderByAsc("number").orderByDesc("create_time");
        }else {
            queryWrapper.orderByDesc("number").orderByDesc("create_time");
        }
        Page<ComputerModel> pages = new Page<>();
        pages.setCurrent(page);
        pages.setSize(limit);
        Page<ComputerModel> comList = computerMapper.selectPage(pages, queryWrapper);
        return comList;
    }

    @Override
    public void update(ComputerModel model) {
        computerMapper.updateById(model);
    }

    @Override
    public void insert(ComputerModel model) {
        computerMapper.insert(model);
    }

    @Override
    public void delete(int id) {
        computerMapper.deleteById(id);
    }

    @Override
    public Integer comCount(List<Integer> idsList) {
        QueryWrapper<ComputerModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id");
        if (idsList != null && idsList.size() > 0) {
            queryWrapper.in("department_id", idsList);
        }
        Integer comCount = computerMapper.selectCount(queryWrapper);
        return comCount;
    }

    @Override
    public List<ComputerModel> getPlantByIds(List<Integer> appIdList) {
        QueryWrapper<ComputerModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().in("id", appIdList);
        List<ComputerModel> personModelList = computerMapper.selectList(queryWrapper);
        return personModelList;
    }

    //办公设备的批量删除
    @Override
    public Result<Object> deleteComputerBatchByIds(String ids) {
        // 参数校验
        if (StringUtils.isEmpty(ids)) {
            return Result.success("参数为空，无需删除");
        }
        List<Integer> idsList = Arrays.stream(ids.split(","))
                .map(Integer::valueOf)
                .collect(Collectors.toList());
        // 批量删除
        if (!ListUtils.isEmpty(idsList)) {
            computerMapper.deleteBatchIds(idsList);
            return Result.success("批量删除成功");
        } else {
            return Result.success("批量删除失败，数据为空");
        }
    }
}
